testing_effect_sensitive = function(.df, .subset = NULL, ...){
  
  if (.subset== "all"){estimation_sample = .df}
  if (.subset== "java"){estimation_sample = .df %>% filter(java_indicator == 1)}
  if (.subset== "nonjava"){estimation_sample = .df %>% filter(java_indicator == 0)}
  
  bws <- seq(1.25, 50, by = 1.25)
  for(i in 1:length(bws)){
    
    temp_sample =
      estimation_sample %>%
      filter(abs(forcing_skd) < bws[i])
    
    temp_sample$zscore = 
      kling_index_f3(.data = temp_sample,
                     .impute = F,
                     ... = ...)
    tmp = 
      temp_sample %>%
      lm_robust(zscore ~ fail_skd, data = .) %>%
      tidy() %>%
      mutate(bw = (bws[i]/5))
    
    if(i == 1){fin = tmp} else{fin = bind_rows(fin,tmp)}
  }
  
  fin = fin %>% filter(term == "fail_skd")
  return(fin)
}


java_index <- testing_effect_sensitive(.df = data_3, .subset = "java", ... = q17_survey_java1, q17_survey_java2)
nonjava_index = testing_effect_sensitive(.df = data_3, .subset = "nonjava", ... = q17_survey_java1, q17_survey_java2)
regional_index = testing_effect_sensitive(.df = data_3, .subset = "all", ... = q18_survey_daerah1, q18_survey_daerah2, q18_survey_daerah3)
religious_index = testing_effect_sensitive(.df = data_3, .subset = "all", ... = q19_survey_relg1, q19_survey_relg2, q19_survey_relg3)
national_index = testing_effect_sensitive(.df = data_3, .subset = "all", ... = q20_survey_pancasila_h3, q21_survey_nation_ethnic_h3)
corruption_index = testing_effect_sensitive(.df = data_3, .subset = "all", ... = q16_survey_transparent_h2, q13_survey_success_reason_1_merit_h2, 
                                            q13_survey_success_reason_3_sara, q13_survey_success_reason_2_connection_h2,
                                            q14_survey_test_vs_connection)




make_testing_sens_plot <- function(df){
  
  df %>%
    ggplot(aes(x=bw, y = estimate)) +
    geom_point() +
    geom_line(color = "red") +
    geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0, color = "black") +
    ylim(-0.45, 0.45) +
    xlab("Bandwidth") +
    theme_bw() +
    geom_hline(yintercept = 0, linetype = "dotted") +
    scale_x_continuous(breaks=c(2, 4, 6, 8, 10),
                       labels=c("2","4","6", "8", "10")) +
    ylab("Estimate (SDs)") +
    theme(panel.grid.minor = element_blank(), 
          legend.position = "none",
          #axis.line.x = element_blank(),
          axis.ticks.x = element_blank(),
          panel.grid.major.x = element_blank(),
          #axis.line.y.left = element_blank(),
          axis.line = element_line(colour = "black"),
          panel.border = element_blank()) %>%
    return()
  
}

java_index <- make_testing_sens_plot(java_index)
nonjava_index <- make_testing_sens_plot(nonjava_index)
regional_index <- make_testing_sens_plot(regional_index)
religious_index <- make_testing_sens_plot(religious_index)
national_index <- make_testing_sens_plot(national_index)
corruption_index <- make_testing_sens_plot(corruption_index)




ggsave(filename = "./_4_outputs/figures/figure_a8a.png", plot = java_index, width = 6, height = 4)
ggsave(filename = "./_4_outputs/figures/figure_a8b.png", plot = nonjava_index, width = 6, height = 4)
ggsave(filename = "./_4_outputs/figures/figure_a8c.png", plot = regional_index, width = 6, height = 4)
ggsave(filename = "./_4_outputs/figures/figure_a8d.png", plot = religious_index, width = 6, height = 4)
ggsave(filename = "./_4_outputs/figures/figure_a8e.png", plot = national_index, width = 6, height = 4)
ggsave(filename = "./_4_outputs/figures/figure_a8f.png", plot = corruption_index, width = 6, height = 4)
